﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Google.Analytics.Web.Converters;

namespace Google.Analytics.Web
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DropDown1 runat=\"server\"> </{0}:DropDown1>")]
    public class CheckboxDropdown : CompositeControl
    {
        private TreeView treeView;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("Extending the DropDown control")]
        [Localizable(true)]
        public virtual String Title
        {
            get
            {
                if (ViewState["Title"] == null)
                    return ("No Title");

                return ((string)ViewState["Title"]);
            }
            set
            {
                ViewState["Title"] = value;
            }
        }

        [TypeConverterAttribute(typeof(CSVConverter))]
        public string[] Children
        {
            get
            {
                if (ViewState["Children"] == null)
                    return new string[0];

                return (string[])ViewState["Children"];
            }
            set
            {
                ViewState["Children"] = value;
            }
        }

        public TreeNodeCollection SelectedItems
        {
            get
            {
                return treeView.CheckedNodes;
            }
        }


        protected override void CreateChildControls()
        {
            treeView = new TreeView
                           {
                               ExpandImageUrl = "~/Images/dropdown.gif",
                               CssClass = "treeviewCss",
                               CollapseImageUrl = "~/Images/dropdown.gif",
                               ShowCheckBoxes = TreeNodeTypes.Leaf
                           };
            var oNode = new TreeNode(Title);

            foreach (var s in Children)
            {
                var oNodeSub = new TreeNode(s);
                oNodeSub.SelectAction = TreeNodeSelectAction.Select;
                oNode.ChildNodes.Add(oNodeSub);
            }

            treeView.Nodes.Add(oNode);
            Controls.Add(treeView);
        }

        [Category("Action"),
        Description("Raised when the user checks.")]
        protected void TreeView_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            //BuildLabel(treeView, label);
        }



        [Category("Action"),
        Description("Raised when the user collapses the treeview.")]
        protected void TreeView_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
        {
            //BuildLabel(treeView, label);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"1", false);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            treeView.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
        }
    }
}
